﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<string>
using namespace std;

//数组中两个字符串的最⼩距离
int main() {
    int n;
    string s1, s2;
    string s;
    cin >> n;
    cin >> s1 >> s2;

    int pstr1 = -1, pstr2 = -1, cur = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
        cin >> s;
        if (s == s1) {
            if (pstr2 != -1) {
                cur = min(cur, i - pstr2);
            }
            pstr1 = i;
        }
        else if (s == s2) {
            if (pstr1 != -1) {
                cur = min(cur, i - pstr1);
            }
            pstr2 = i;
        }
    }
    if (cur == 0x3f3f3f3f) cout << -1;
    else  cout << cur;

    return 0;

}